<?php

/**
 * 短信通知记录
 */
class SmslogController extends Controller_Base
{
    /**
     * 短信列表
     */
    public function indexAction()
    {
        $request = $this->getRequest();
        $type = intval($request->getQuery('type', 1));
        $page = intval($request->getQuery('page', 1));
        $sendStatus = $request->getQuery('sendStatus', 0); //短信状态
        $mobile = trim($request->getQuery('mobile', 0)); //短信状态
        $litigantName = trim($request->getQuery('litigantName', 0)); //短信状态
        $scene = $request->getQuery('scene', ''); //场景类型
        $limit = intval($request->getQuery('limit', 10));
        $mediationOrganId = trim($request->getQuery('mediationOrganId', 0));
        $mediatorId = $request->getQuery('mediatorId', 0);
        $is_show = intval($request->getQuery('is_show', 0));
        $caseMediationOrganId = intval($request->getQuery('caseMediationOrganId', 0));
        $importRecordId = $request->getQuery('importRecordId', 0);
        $batshId = $request->getQuery('batshId', ''); //案件标识号
        $assetsId = $request->getQuery('assetsId', 0);
        $case_id = $request->getQuery('case_id', 0);


        $page = $page ? $page : 1;
        $start = ($page - 1) * $limit;

        $query = array('limit' => $limit);
        $query['mediationOrganId'] = $mediationOrganId;
        $query['type'] = $type;

        if (!empty($begin_date) || !empty($end_date)) {
            $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
            $end_date = $end_date ? $end_date . " 23:59:59" : "";
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        $query['add_begin_end'] = $add_begin_end;
        if($add_begin_end){
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
        }


        if ($type == 1) {
            $where = $this->getWhere('sms_record.mediatorId',[],"sms_record.mediationOrganId");
            if ($mediationOrganId) {
                array_push($where, "sms_record.mediationOrganId = " . $mediationOrganId);
                $query['mediationOrganId'] = $mediationOrganId;
            }
            if ($mobile) {
                array_push($where, "sms_record.mobile = " . $mobile);
                $query['mobile'] = $mobile;
            }
            if ($litigantName) {
                $caseId = $this->getCaseIdByLitigantName($litigantName);
                if (!empty($caseId)) {
                    array_push($where, "sms_record.caseId in ({$caseId})");
                }else{
                    array_push($where, "sms_record.caseId =0");
                }
                $query['litigantName'] = $litigantName;
            }
            if ($sendStatus) {//发送状态
                array_push($where, "sms_record.sendStatus = '{$sendStatus}'");
                $query['sendStatus'] = $sendStatus;
            }

            if ($scene) {//发送状态
                array_push($where, "sms_record.scene = '{$scene}'");
                $query['scene'] = $scene;
            }

            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "sms_record.sendTime <= {$end_date}");
            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "sms_record.sendTime >= {$begin_date}");
            }
            if ($importRecordId) {
                array_push($where, "cases.importRecordId = {$importRecordId}");
                $query['importRecordId'] = $importRecordId;
            }
            if ($assetsId) {
                array_push($where, "cases.assetsId = {$assetsId}");
                $query['assetsId'] = $assetsId;
            }
            if ($caseMediationOrganId) {//按照案件号查询
                array_push($where, "cases.mediationOrganId = {$caseMediationOrganId}");
                $query['caseMediationOrganId'] = $caseMediationOrganId;

            }
            if ($case_id) {//按照案件号查询
                array_push($where, "sms_record.caseId = {$case_id}");
                $query['case_id'] = $case_id;

            }
            if ($batshId) {//按照案件号查询
                array_push($where, "cases.batshId = '{$batshId}'");
                $query['batshId'] = $batshId;

            }
            if ($mediatorId) {//按照案件号查询
                array_push($where, "sms_record.mediatorId = {$mediatorId}");
                $query['mediatorId'] = $mediatorId;
            }
            $seacher = array(
                'where' => $where,
                'limit' => $limit,
                'start' => $start,
                'join' => 'left join cases on sms_record.caseId = cases.id',
                'order' => 'id DESC'
            );
            $cases = Module_Mediate_SmsRecord::getInstance()->getAllBySearch(array("sms_record.*"), $seacher, true);
            $total = Module_Mediate_SmsRecord::getInstance()->getAllBySearch(array("count(sms_record.id) as num,sum(sms_record.billableNum) as billableNum"), array('where' => $where, 'join' => 'left join cases on sms_record.caseId = cases.id'), true);

            $billableNum = $total[0]['billableNum'];
            SmartyAdapter::assign('billableNum', $billableNum ? $billableNum : 0);
            $total = $total[0]['num'];
        } else {
            $where = $this->getWhere();

            if ($mediationOrganId) {
                array_push($where, "mediationOrganId = " . $mediationOrganId);
                $query['mediationOrganId'] = $mediationOrganId;
            }
            if ($mobile) {
                array_push($where, "mobile = " . $mobile);
                $query['mobile'] = $mobile;
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "replyTime <= {$end_date}");
            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "replyTime >= {$begin_date}");
            }
            if ($litigantName) {
                $caseId = $this->getCaseIdByLitigantName($litigantName);
                if (!empty($caseId)) {
                    array_push($where, "caseId in ({$caseId})");
                }else{
                    array_push($where, "caseId =0");
                }
                $query['litigantName'] = $litigantName;
            }
            $seacher = array(
                'where' => $where,
                'limit' => $limit,
                'start' => $start,
                'order' => 'id DESC'
            );
            $cases = Module_Mediate_SmsReplyRecord::getInstance()->getAllBySearch(array("*"), $seacher, true);
            $total = Module_Mediate_SmsReplyRecord::getInstance()->getAllBySearch(array("count(*) as num"), array('where' => $where), true);

            $total = $total[0]['num'];
        }
        $this->getMediator();
        $this->getAssets();
        $mediationOrganList = Module_InfoBase_MediationOrgan::getInstance()->getAll();
        $mediationOrganList = array_column($mediationOrganList, null, 'id');
        $totalPage = ceil($total / $limit);
        SmartyAdapter::assign('mediationOrganList', $mediationOrganList);
        SmartyAdapter::assign('arrList', $cases);
        SmartyAdapter::assign('scene', Lib_SMS_Config::$smsCode);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('type', $type);
        SmartyAdapter::assign('is_show', $is_show);
        SmartyAdapter::assign('page_query', http_build_query($query));

    }

}
